package com.itpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itpeng.Utils.POIUtils;
import com.itpeng.constant.MessageConstant;
import com.itpeng.entity.Result;
import com.itpeng.pojo.OrderSetting;
import com.itpeng.service.OrderSettingService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/23  20:33
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;


    /**
     * 批量导入预约设置信息
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //读取excel文件,获得每一行的数据
            List<String[]> strings = POIUtils.readExcel(excelFile);
            //将获得的每一行的数据存放到OrderSetting对象中
            List<OrderSetting> orderSettingList = new ArrayList<>();
            OrderSetting orderSetting = null;
            //日期格式解析
            SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);
            for (String[] string : strings) {
                Date orderDate = sdf.parse(string[0]);
                Integer number = Integer.valueOf(string[1]);
                //创建OrderSetting对象
                orderSetting = new OrderSetting(orderDate,number);
                //将OrderSetting对象存入集合
                orderSettingList.add(orderSetting);
            }

            //调用service层方法，将预约人数批量进行设置
            orderSettingService.add(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据年月查询当月预约设置
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        List<Map<String, Integer>> orderSettingList  = orderSettingService.getOrderSettingByMonth(month);

        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettingList);
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
